package kr.co.icebird.temp;

import java.io.StringWriter;
import java.util.List;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import kr.co.icebird.spring3.common.model.Menu;
import kr.co.icebird.spring3.common.service.MenuService;
import kr.co.icebird.spring3.common.service.MenuServiceImpl;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;

public class XMLEx01 {

	public static void main(String[] args) throws Exception {
		
		MenuService impl = new MenuServiceImpl();
		
		List<Menu> ll = impl.getMenuList(); 
		
		Document doc = null;
		DocumentBuilderFactory factory = null;
		factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		doc = builder.newDocument();
		
		Element prj_list = doc.createElement("ul");
		doc.appendChild(prj_list);
		
		for (Menu mm : ll) {

			Element l = doc.createElement("AA"+mm.getMenuId().toString());
			prj_list.appendChild(l);
		}

		
		Element prj_info = doc.createElement("li");
		prj_list.appendChild(prj_info);
		
		Element element = doc.createElement("test_element");
		Text textNode = doc.createTextNode("테스트 입니다");
		prj_info.appendChild(textNode);
		
		TransformerFactory tr_factory = TransformerFactory.newInstance();
		String xmlStr = "";     
		try
		{
		    StringWriter sw = new StringWriter();
		    Properties output = new Properties();
		    output.setProperty(OutputKeys.INDENT, "yes");
		    output.setProperty(OutputKeys.ENCODING, "EUC-KR");
		    Transformer transformer = tr_factory.newTransformer();
		    transformer.setOutputProperties(output);
		    transformer.transform(new DOMSource(doc), new StreamResult(sw));
		    xmlStr = sw.getBuffer ().toString ();
		}
		catch (Exception e)
		{
		    e.printStackTrace();
		}
		
		System.out.println(xmlStr);
	}

}
